package com.quanyan.user.utils;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2016/5/26.
 */
public class ValidUtils<T> {

    /**
     * 正则表达式：验证手机号
     */
    public static final String REGEX_MOBILE = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";


    public static final String REGEX_MOBILE_ELEVEN = "^1\\d{10}$";


    /**
     * 校验手机号
     *
     * @param mobile
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isRegMobile(String mobile) {
        if(mobile != null)
            return Pattern.matches(REGEX_MOBILE_ELEVEN, mobile);
        else
            return false;
    }
    /**
     * 验证实体
     * @param t
     * @return
     */
    public  List<String> getListMessage(T t){
        ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
        Validator validator = vf.getValidator();
        Set<ConstraintViolation<T>> set = validator.validate(t);
        if(set.size()< 1)
            return null;
        List<String> lsgMsg = new ArrayList<String>();
        for (ConstraintViolation<T> constraintViolation : set) {
            lsgMsg.add(constraintViolation.getMessage());
        }
        return lsgMsg;
    }
}
